package service;

import common.CacheService.SingleCommonCacheService;
import common.Pagination;
import dao.CroAddressDao;
import entity.CroAddress;
import entity.CroCustomer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xuweijie on 2017/4/7.
 */
@Service
public class CroAddressService {

    @Autowired
    private CroAddressDao croAddressDao;

    @Autowired
    private SingleCommonCacheService<CroAddress> croAddressSingleCommonCacheService;

    /**
     * 根据Id查找地址
     * @param id Id
     * @return Address
     */
    @Transactional(readOnly = true)
    public CroAddress findAddressById(String id){
        return croAddressDao.getById(id);
    }

    /**
     * 根据顾客查找地址（不分页）
     * @param croCustomer 顾客
     * @return list
     */
    @Transactional(readOnly = true)
    public List<CroAddress> listAddressByCustomer(CroCustomer croCustomer){
        Map<String,Object> map=new HashMap<>();
        map.put("costemId",croCustomer);
        map.put("del_flag","0");
        return croAddressDao.findByProperty(map);
    }

   /*
   * 根据顾客实体查找出地址信息（分页）
   * */
   @Transactional(readOnly = true)
   public Pagination<CroAddress> FindAddressByCusId(CroCustomer croCustomer,int index,int size ){
       Map<String,Object> map=new HashMap<>();
       map.put("costemId",croCustomer);
       map.put("del_flag","0");
       Pagination<CroAddress> croAddresses=croAddressDao.findPaginationByProperty(map,index,size);
       return croAddresses;
   }

   /*
   * 根据id删除地址信息
   * */
   @Transactional(readOnly = false)
   public boolean DeleteAddressById(String addrid,String del_flag){
       try {
           CroAddress croAddress=croAddressDao.getById(addrid);
           croAddress.setDel_flag(del_flag);
           croAddressDao.update(croAddress);
           return true;
       }catch (Exception e){
           e.printStackTrace();
           return false;
       }
   }

   /*
   * 更新地址信息
   * */
   @Transactional(readOnly = false)
   public boolean UpdateAddressByEntity(CroAddress croAddress){
       try{
           croAddressDao.update(croAddress);
           return true;
       }catch (Exception e){
           e.printStackTrace();
           return false;
       }
   }

   /*
   * 插入新地址
   * */
   @Transactional(readOnly = false)
   public boolean InsertAddressByEntity(CroAddress croAddress){
       try {
           croAddressDao.insert(croAddress);
           return true;
       }catch (Exception e){
           e.printStackTrace();
           return false;
       }
   }


//    @Transactional(readOnly = true)
//    public void find(){
//        String id="8a8e88325bb39d7e015bb39dddc70000";
//        String key="key_address_1";
//
//        if(croAddressCommonCacheService.getCacheObject(key)==null){
//            System.out.print("插入缓存");
//            CroAddress croAddress=croAddressDao.getById(id);
//            croAddressCommonCacheService.setCacheObject(key,croAddress);
//        }else{
//            System.out.print("读取缓存");
//            CroAddress croAddress=croAddressCommonCacheService.getCacheObject(key);
//            System.out.print("aaa:"+croAddress.getConsignee()+"  id"+croAddress.getId());
//        }
//
//    }

}
